package controllers.listeners.semrep;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import objects.Feature;

import models.FeaturesModel;
import views.MainPanel;

/* Edits the value of the selected feature */
/* actionPerformed when you click on the features table */
public class TableMouseListener implements MouseListener{

	MainPanel view;
	
	public void setView(MainPanel view){
		this.view = view;
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		int clickCount = arg0.getClickCount();
		JTable table = (JTable) arg0.getSource();
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		
		int row = table.getSelectedRow();
		int column = table.getSelectedColumn();
		String featureName = (String) tableModel.getValueAt(row, tableModel.findColumn("Name"));
		String featureValue = (String) tableModel.getValueAt(row, tableModel.findColumn("Value"));

		if (clickCount < 2) {
			return;
		}
		
		if(column != 0 && !featureName.equals(" ")){
			ArrayList<objects.Constituent> db = null;
			
			try {
				db = FeaturesModel.getFeatures();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
			ArrayList<String> choices = new ArrayList<String>();
			for(int i = 0; i < db.size(); i++) {
				if(db.get(i).getLabel().equalsIgnoreCase(view.getCurrentConstituent()))
					for(int j = 0; j < db.get(i).getFeatures().getFeatureList().size(); j++)
						if(db.get(i).getFeatures().getFeatureList().get(j).getName().equalsIgnoreCase(featureName)){
							
							choices = db.get(i).getFeatures().getFeatureList().get(j).getValueList();
						}
			}
			
			Object[] possibilities = choices.toArray();
			String s = (String)JOptionPane.showInputDialog(new JFrame(), "Select the new value of the feature '" + featureName + "'.",
			                    "Edit Feature", JOptionPane.PLAIN_MESSAGE, null, possibilities, featureValue);
			if(s != null && !featureValue.equals(s) && !s.isEmpty()){
				tableModel.setValueAt(s, row, column);
				int i = 0;
				for(i = 0; i < view.getCurrCon().getFeatures().getFeatureList().size(); i++)
					if(view.getCurrCon().getFeatures().getFeatureList().get(i).getName().equals(featureName)){
						if(s.equals(choices.get(0))){
							Feature defaultFeature = new Feature();
							defaultFeature.setName(featureName);
							defaultFeature.setValue(s);
							
							for(int z = 0; z < view.getCurrCon().getFeatures().getFeatureList().size(); z++)
								if(view.getCurrCon().getFeatures().getFeatureList().get(z).getName().equals(s))
									view.getCurrCon().getFeatures().getFeatureList().remove(z);
							break;
						}
						else{
							view.getCurrCon().getFeatures().getFeatureList().get(i).setValue(s);
							break;
						}
					}
				
				
				if(i == view.getCurrCon().getFeatures().getFeatureList().size()){
					Feature newFeature = new Feature();
					newFeature.setName(featureName);
					newFeature.setValue(s);
					view.getCurrCon().getFeatures().addFeatureListItem(newFeature);
				}
			}

		}
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}

}
